<?php

namespace App\Listeners;

use App\Events\ProfitShare;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Http;
use App\Models\ProfitSharingAccount;
use Illuminate\Support\Facades\Log;

class PayNotify
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  ProfitShare  $event
     * @return void
     */
    public function handle(ProfitShare $event)
    {
        $notifyData = $event->notifyData;
        $subAppId = isset($notifyData['sub_appid']) ? $notifyData['sub_appid'] : $notifyData['appid'];
        $subMchId = isset($notifyData['sub_mch_id']) ? $notifyData['sub_mch_id'] : $notifyData['mch_id'];
        $wxProfitSharingAccount = ProfitSharingAccount::where('sub_appid', $subAppId)->where('sub_mchid', $subMchId)->first();

        Log::info('PayNotify Listener------begin');
        Log::info($wxProfitSharingAccount);
        Log::info('PayNotify Listener------end');

        if(!empty($wxProfitSharingAccount) and $wxProfitSharingAccount->pay_notify_url){
            Http::post($wxProfitSharingAccount->pay_notify_url, $event->notifyData);
        }
    }
}
